package com.noah.bee.admin.web.controller;

import com.noah.bee.admin.Zookeeper.ZkClientService;
import com.noah.bee.admin.constant.TabDefinition;
import com.noah.bee.admin.domain.User;
import com.noah.bee.admin.servlet.constants.GlobalConstants;
import com.noah.bee.admin.web.pojo.ConfigInfo;
import com.noah.bee.admin.web.pojo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author wuzhen
 * @since 16/12/19 15:32
 */

@Controller
@RequestMapping("/")
public class BeeController {

    @Autowired
    private ZkClientService zkClientService;

    /**
     * 渲染系统首页
     */
    @RequestMapping(value = {"", "index"})
    public ModelAndView index(HttpSession session) {
        return buildMav(TabDefinition.Introduction.getViewName(), TabDefinition.Introduction.getCode(), session);
    }

    /**
     * 渲染主页某个tab界面
     */
    @RequestMapping(value = "settings")
    public ModelAndView settingsList(HttpServletRequest request) throws Exception {
        int active = null == request.getParameter("active") ? TabDefinition.Introduction.getCode() : Integer.parseInt(request.getParameter("active"));
        TabDefinition tabDefinition = TabDefinition.getTabDefinition(active);
        String viewName = tabDefinition.getViewName();
        String appName = tabDefinition.name();
        ModelAndView mav= buildMav(StringUtils.isEmpty(viewName) ? TabDefinition.Introduction.getViewName() : viewName, active, request.getSession());
        if(active != 0){
            // 从zk中获取节点数据
            Map<String, String> map =  zkClientService.getDataAndWatch(appName);
            mav.addObject("configs",map);
        }
        return mav;
    }

    /**
     * 新增设置数据
     */
    @ResponseBody
    @RequestMapping(value = "/api/save/{appName}")
    public Msg settings(@PathVariable("appName") int appName, @RequestParam(value = "name") String name, @RequestParam(value = "value") String value) throws Exception {
        TabDefinition tabDefinition = TabDefinition.getTabDefinition(appName);
        String path = zkClientService.createNodeAndData(tabDefinition.name(), name, value);
        return new Msg(true, path);
    }

    @ResponseBody
    @RequestMapping(value = "/api/get/{appName}/{key}")
    public Msg getConfig(@PathVariable("appName") int appName, @PathVariable("key") String configName){
        TabDefinition tabDefinition = TabDefinition.getTabDefinition(appName);
        ConfigInfo configInfo = zkClientService.getConfig(tabDefinition.name(), configName);
        return new Msg(true, configInfo);
    }

    @ResponseBody
    @RequestMapping(value = "/api/update/{appName}")
    public Msg update(@PathVariable("appName") int appName, @RequestParam(value = "update_window_name") String name, @RequestParam(value = "update_window_value") String value) throws Exception {
        TabDefinition tabDefinition = TabDefinition.getTabDefinition(appName);
        zkClientService.updateConfig(tabDefinition.name(), name, value);
        return new Msg(true, "");
    }

    private ModelAndView buildMav(String viewName, int active, HttpSession session) {
        ModelAndView mav = new ModelAndView(viewName);
        User user = (User) session.getAttribute(GlobalConstants.USER_SESSION);
        mav.addObject("username", user.getUserName());
        mav.addObject("tabs", TabDefinition.values());
        mav.addObject("active", active);
        return mav;
    }

}
